<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('公告新增', '', '')"/>
    <th:block th:include="include :: wangeditor_css"/>
</head>
<body>
<form class="layui-form">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label">标题</label>
                    <div class="layui-input-block">
                        <input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="公告标题" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">类型</label>
                    <div class="layui-input-block">
                        <select name="type" id="type">
                            <option value="1">一般</option>
                            <option value="2">重要</option>
                            <option value="3">弹窗</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">内容</label>
                    <div class="layui-input-block">
                        <!-- 编辑器 DOM -->
                        <div style="border: 1px solid #ccc;">
                            <div id="editor-toolbar" style="border-bottom: 1px solid #ccc;"></div>
                            <div id="editor-text-area" style="height: 500px"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit="" lay-filter="sysInterUrl-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<th:block th:include="include :: footer"/>
<script>
    layui.use(['form', 'jquery'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let MODULE_PATH = rootPath+"/SysNoticeController";
        //名称验证
        form.verify({
            interName: function(value){
                if(value.length < 2){
                    return '至少得2个字符';
                }
                var msg = "";
                $.ajax({
                    url: MODULE_PATH+'/checkNameUnique',
                    data: "interName="+value,
                    dataType: 'json',
                    type: 'post',
                    async:false,
                    success: function (result) {
                            msg=result;
                    }
                });
                if (msg == 1) {
                   return "已有相同数据";
                }

            }
        });

        //保存
        form.on('submit(sysInterUrl-save)', function (data) {
            data.field.content = editor.getHtml();
            $.ajax({
                url: MODULE_PATH+'/add',
                data: data.field,
                dataType: 'json',
                contentType: 'application/x-www-form-urlencoded',
                type: 'post',
                success: function (result) {
                    if (result.code==200) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload("sysNotice-table");
                        });
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000});
                    }
                }
            })
            return false;
        });
    })
</script>

<th:block th:include="include :: wangeditor"/>
<script>
    $(function () {

        const E = window.wangEditor;
        const editorConfig = {
            MENU_CONF: {
                'uploadImage': {
                    server: '/upload-img', // 上传图片地址
                    timeout: 10 * 1000, // 5s
                    fieldName: 'file',
                    // meta: {token: 'xxx', a: 100},
                    // metaWithUrl: true, // 参数拼接到 url 上
                    // headers: {Accept: 'text/x-json'},

                    maxFileSize: 10 * 1024 * 1024, // 10M

                    base64LimitSize: 5 * 1024, // 5kb 以下插入 base64

                    onBeforeUpload(files) {
                        console.log('onBeforeUpload', files)

                        return files // 返回哪些文件可以上传
                        // return false 会阻止上传
                    }
                }
            }
        }
        window.editor = E.createEditor({
            selector: '#editor-text-area',
            html: '',
            mode: 'simple',
            config: editorConfig
        })

        window.toolbar = E.createToolbar({
            editor,
            mode: 'simple',
            selector: '#editor-toolbar',
            config: {}
        });
    });
</script>
</body>
</html>